package com.cskaoyan.javase.practice.stage6.utils;

import com.cskaoyan.javase.practice.stage5.entity.Student;

import java.util.Scanner;

/**
 * @program: little-practice
 * @description: get data from keyboard scanner
 * @author: Keyu Li
 * @create: 2021-09-06 21:07
 **/

public class ScannerUtils {
    private static Scanner sc = new Scanner(System.in);

    private ScannerUtils() {

    }

    // 已有id新建学生对象
    public static Student getInstanceWithIdByScanner(int id) {
        return new Student(id, getNameByScanner(), getGenderByScanner(),
                getAgeByScanner(), getGradeByScanner(Student.GRADE_END),
                getTeleByScanner(Student.LEGAL_TELE_HEADS, Student.LEGAL_TELE_LENGTH));
    }

    // 接收id 并检验合法性
    public static int getIdByScanner() {
        int id = -1;
        while (true) {
            System.out.print("请输入正整数学生ID： ");
            try {
                id = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalId(id)) break;
        }
        System.out.println();
        return id;
    }

    // 接收姓名
    public static String getNameByScanner() {
        System.out.print("请输入姓名： ");
        String name = sc.nextLine().trim();
        System.out.println();
        return name;
    }

    // 接收性别 并检验合法性
    public static String getGenderByScanner() {
        String gender;
        while (true) {
            System.out.print("请输入性别（男/女）： ");
            gender = sc.nextLine().trim();
            if (checkLegalGender(gender)) break;
            System.out.println("输入格式不合法！");
        }
        System.out.println();
        return gender;
    }

    // 接收年龄 并检验合法性
    public static int getAgeByScanner() {
        int age = -1;
        while (true) {
            System.out.print("请输入学生年龄[0~150)： ");
            try {
                age = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalAge(age)) break;
            System.out.println("输入格式不合法！");
        }
        System.out.println();
        return age;
    }

    // 接收年纪 并检验合法性
    public static String getGradeByScanner(String endStr) {
        String grade;
        while (true) {
            System.out.print("请输入班级（以\"" + endStr + "\"结尾）： ");
            grade = sc.nextLine().trim();
            if (checkLegalGrade(grade, endStr)) break;
            System.out.println("输入格式不合法！");
        }
        System.out.println();
        return grade;
    }

    // 接收电话 并检验合法性
    public static String getTeleByScanner(String[] legalTele, int ll) {
        String tele = null;
        while (true) {
            System.out.print("请输入以 ");
            for (String s : legalTele) {
                System.out.print(s+", ");
            }
            System.out.println(" 开头的"+ll+"位电话： ");
            tele = sc.nextLine().trim();
            if (checkLegalTele(tele, legalTele, ll)) break;
            System.out.println("输入格式不合法！");
        }
        System.out.println();
        return tele;
    }

    // 接收菜单 并检验合法性
    public static int getMenuChoice(int range) {
        int choice = -1;
        while (true) {
            System.out.print("请选择序号（1-"+range+"）： ");
            try {
                choice = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalChoice(choice, range)) break;
            System.out.println("输入格式不合法！");
        }
        System.out.println();
        return choice;
    }

    // 接收确认标志 并检验合法性
    public static boolean getConfirmFlag() {
        int choice = -1;
        while (true) {
            System.out.print("1 确认；2 取消： ");
            try {
                choice = Integer.parseInt(sc.nextLine());
            } catch (NumberFormatException nfe) {
                continue;
            }
            if (checkLegalConfirm(choice)) break;
            System.out.println("输入格式不合法！");
        }
        System.out.println();
        return choice==1;
    }

    // 检查id的合法性
    static boolean checkLegalId(int id) {
        return id > 0;
    }

    // 检查性别的合法性
    static boolean checkLegalGender(String gender) {
        return "男".equals(gender) || "女".equals(gender);
    }

    // 检查年龄的合法性
    static boolean checkLegalAge(int age) {
        return age >= 0 && age < 150;
    }

    // 检查班级的合法性
    static boolean checkLegalGrade(String grade, String endStr) {
        return grade.endsWith(endStr);
    }

    // 检查电话的合法性
    static boolean checkLegalTele(String tele, String[] teleHeads, int teleL) {
        if (tele.length() != teleL) return false;
        for (String head : teleHeads) {
            if (tele.startsWith(head))
                return true;
        }
        return false;
    }

    // 检查菜单选择的合法性
    static boolean checkLegalChoice(int choice, int range) {
        return choice > 0 && choice <= range;
    }

    // 检查确认选项的合法性
    static boolean checkLegalConfirm(int choice) {
        return choice == 1 || choice == 2;
    }
}
